var fs = require("fs");
var vm = require("vm");

var code = fs.readFileSync("../src/util.js");
vm.runInThisContext(code, context);

var should = require("should");

module.exports = function() {
    it("isFunction判断是否函数", function() {
        util.isFunction(function() {}).should.ok;
        util.isFunction(new Function()).should.ok;
    });

    it("validateName 验证空间名称和模块名称", function() {
        (function() {
            util.validateName("abcd1234");
            util.validateName("1234abcd");
            util.validateName("_");
            util.validateName("A");
            util.validateName("1234");
            util.validateName("_Abcd1234");
            util.validateName("_Ab.cd.1234");
        }).should.not.throw();

        (function() {
            util.validateName("abcd$1324");
        }).should.throw("[jNs] unexpect character in name: $");

        (function() {
            util.validateName("AB,,.213$$4");
        }).should.throw("[jNs] unexpect character in name: ,,");
    });

    it("splitFullName 拆分模块名称的命名空间部分和模块名称部分", function() {
        util.splitFullName("a.b.c.d.e.f").should.eql({
            name: "f",
            ns: "a.b.c.d.e"
        });

        util.splitFullName("ns.").should.eql({
            name: "",
            ns: "ns"
        });

        util.splitFullName("module").should.eql({
            name: "module",
            ns: ""
        });

        util.splitFullName(".module").should.eql({
            name: "module",
            ns: ""
        });

        util.splitFullName(".").should.eql({
            name: "",
            ns: ""
        });

        util.splitFullName("").should.eql({
            name: "",
            ns: ""
        });
    });

    it("splitFullName 不验证也不校正名称", function() {
        util.splitFullName("....").should.eql({
            name: "",
            ns: "..."
        });

        util.splitFullName(" abc. 123 . @#$. a $name ").should.eql({
            name: " a $name ",
            ns: " abc. 123 . @#$"
        });
    });
};
